//
//  SKTabBarViewController.m
//  loveKangaroo
//
//  Created by SeeKool on 15/3/3.
//  Copyright (c) 2015年 SeeKool Inc. All rights reserved.
//

#import "SKTabBarViewController.h"
#import "SKHomeViewController.h"
#import "SKDiscoverViewController.h"
#import "SKCreatNewViewController.h"
#import "SKCurrentPlanViewController.h"
#import "SKMeViewController.h"
#import "SKTabBar.h"

@interface SKTabBarViewController ()<SKTabBarDelegate>
@property (nonatomic, weak) SKTabBar *iTabBar;
@end

@implementation SKTabBarViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        //1.首页
        SKHomeViewController *home = [[SKHomeViewController alloc] init];
        [self addOneChildViewController:home WithTitle:@"首页" NorImg:@"brushTeeth_nor" SelImg:@"brushTeeth_sel"];
        
        //2.发现
        SKDiscoverViewController *discover = [[SKDiscoverViewController alloc] init];
        [self addOneChildViewController:discover WithTitle:@"发现" NorImg:@"discover_nor" SelImg:@"discover_sel"];
        
//        //3.创建"好东西"
//        SKCreatNewViewController *creatNew = [[SKCreatNewViewController alloc] init];
//        [self addOneChildViewController:creatNew WithTitle:@"+" NorImg:@"achievement_nor" SelImg:@"achievement_sel"];
        
        //4.当前计划
        SKCurrentPlanViewController *currentPlan = [[SKCurrentPlanViewController alloc] init];
        [self addOneChildViewController:currentPlan WithTitle:@"当前计划" NorImg:@"me_nor" SelImg:@"me_sel"];
        
        //5.我的
        SKMeViewController *me = [[SKMeViewController alloc] init];
        [self addOneChildViewController:me WithTitle:@"我的" NorImg:@"me_nor" SelImg:@"me_sel"];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //添加自定义tabBar
    [self setupCustomBar];
    
}
/**
 *  添加自定义tabBar
 */
- (void)setupCustomBar{
    //创建自定义的tabBar
    SKTabBar *iTabBar = [[SKTabBar alloc] init];
    iTabBar.frame = self.tabBar.bounds;
    //设置代理
    iTabBar.delegate = self;
    [self.tabBar addSubview:iTabBar];
    self.iTabBar = iTabBar;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    //移除系统tabBar上创建的按钮
    for (UIView *subView in self.tabBar.subviews) {
        if ([subView isKindOfClass:[UIControl class]]) {
            
            [subView removeFromSuperview];
        }
    }
}
- (void)addOneChildViewController:(UIViewController *)childController WithTitle:(NSString *)title NorImg:(NSString *)norImgName SelImg:(NSString *)selImgName{
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:childController];
//    childController.title = title;
    
    // 让iOS7 导航控制器不透明
    nav.navigationBar.translucent = NO;
    nav.navigationBar.barTintColor = [UIColor yellowColor];
    nav.navigationBar.tintColor = [UIColor blueColor];
    
    
    NSMutableDictionary *md = [NSMutableDictionary dictionary];
    md[NSForegroundColorAttributeName] = [UIColor redColor];
    md[NSFontAttributeName] = [UIFont boldSystemFontOfSize:26];
    [nav.navigationBar setTitleTextAttributes:md];
    
    
    childController.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:[UIImage imageNamed:norImgName] selectedImage:[UIImage imageNamed:selImgName]];
    [self addChildViewController:nav];
    //添加一个自定义按钮
    [self.iTabBar addOneBtnWithItem:childController.tabBarItem];
    
}
- (void)tabBarDidSelectedFromIndex:(NSInteger)from toIndex:(NSInteger)to{
    
    self.selectedIndex = to;
    
}
//- (void)tabBarDidClickCreatNewBtn:(SKTabBar *)tabBar{
//    //1.创建控制器
//    SKCreatNewViewController *creatNewVc = [[SKCreatNewViewController alloc] init];
//    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:creatNewVc];
//    creatNewVc.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(backToPreviousVc)];
//    
//    //2.弹出控制器
//    [self presentViewController:nav animated:YES completion:nil];
//}
//- (void)backToPreviousVc{
//    //关闭控制器
//    [self dismissViewControllerAnimated:YES completion:nil];
//}
@end
